package 力扣日常刷题.木23一月.第04天304;

import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2022/3/4
 * @description
 */
public class Demo42优势洗牌 {

    public int[] advantageCount(int[] A,int[] B) {
        Arrays.sort(A);
        int n = A.length;

        int[][] sortB = new int[n][2];
        for (int i = 0; i < n; i++) {
            sortB[i] = new int[]{B[i],i};
        }
        Arrays.sort(sortB,(a,b)->a[0] - b[0]);

        int[] res = new int[n];
        int left = 0;
        int right = n - 1;

        for (int i = 0; i < n; i++) {
            if(A[i] <= sortB[left][0]) {
                res[sortB[right --][1]] = A[i];
                res[sortB[left++][1]] = A[i];
            }
        }
        return res;
    }

}
